<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 */
namespace Home\Controller;
use Think\AjaxPage;
use Think\Page;
use Think\Verify;
use Home\Logic\ProductLogic;

class ProductController extends BaseController {

	public function index() {

		$this -> display();

	}

	public function details() {
		$id = I('get.id');
		$ret = M('product') -> where('id = ' . $id) -> find();
		$baseinfo = json_decode($ret['p_baseinfo'], true);
		$trafficinfo = json_decode($ret['p_trafficinfo'], true);

		$schedule = json_decode($ret['p_schedule'], true);
		$list = M('product') -> select();
		//推荐产品
		foreach ($list as $key => $value) {
			$list[$key]['p_baseinfo'] = json_decode($value['p_baseinfo'], true);
		}

		// 增加点击率
		M('product') -> where("id =$id") -> setInc('click_num');

		$this -> assign('baseinfo', $baseinfo);
		$this -> assign('trafficinfo', $trafficinfo);
		$this -> assign('schedule', $schedule);
		$this -> assign('list', $list);
		$this -> display();
	}

	//产品列表
	public function lists() {
		$where = "1 = 1 ";
		$where = $_GET['topic'] == "" ? $where : $where . 'and topic = "' . $_GET['topic'] . '"';
		$otype = $_GET['otype'] == "" ? 'id' : $_GET['otype'];
		$order = $_GET['order'] == "" ? 'desc' : $_GET['order'];

		$count = M('product') -> where("status=1 and topic='{$topic}'") -> count();

		// 查询满足要求的总记录数
		$Page = new \Think\Page($count, 8);

		// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show = $Page -> show();
		// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = M('product') -> where($where) -> order("$otype $order") -> limit($Page -> firstRow . ',' . $Page -> listRows) -> select();

		foreach ($list as $key => $value) {

			$list[$key]['p_baseinfo'] = json_decode($value['p_baseinfo'], true);
			$list[$key]['p_trafficinfo'] = json_decode($value['p_trafficinfo'], true);
			//$list[$key]['p_schedule'] = json_decode($value['p_schedule'], true);
		}
		// 赋值数据集
		$this -> assign('page', $show);
		// 赋值分页输出
		$this -> assign('total_page', $Page -> totalPages+1);
		//		var_dump($list[0]['p_baseinfo']);
		//		exit;
		$this -> assign('list', $list);

		$this -> display();

	}

	public function submitorder() {

	}

	public function article() {

		$this -> display();

	}

	private function build_order_no() {
		return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
	}

	public function submit_orders() {

		if (empty($_SESSION['user']['user_id'])) {
			$back['status'] = -1;
			$back['msg'] = "请先登录";

		} else {
			$data = $_POST;
			$product = M('product') -> where("id = {$data['product_id']}") -> find();

			$data['user_id'] = $_SESSION['user']['user_id'];
			$ret = M('orders') -> where("user_id = {$data['user_id']} and product_id = {$data['product_id']}") -> find();
			if (!empty($ret)) {
				$back['status'] = -1;
				$back['msg'] = '您已经提交过订单了,请耐心等待客服联系您';
			} else {
				$data['order_sn'] = $this -> build_order_no();
				$data['ordertime'] = time();
				$data['price'] = $product['price'];
				$data['starttime'] = strtotime($data['datetime']);
				$ret = M('orders') -> add($data);

				if ($ret !== FALSE) {
					$back['status'] = 1;
					$back['msg'] = '提交订单成功';
				} else {
					$back['status'] = -1;
					$back['msg'] = '提交订单失败';

				}
			}
		}
		$this -> ajaxReturn($back);
	}

}
